﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json.Linq;

namespace JiraConnector
{
    /// <summary>
    /// Data object from JSON to Object
    /// </summary>
    public class JIRAIssue
    {
        public String expand;
        public String id;
        public String self;
        public string key;
        public JContainer fields;


        #region "Times"
       
        public decimal timeestimateHrs()
        {
            return convertToHrs("timeestimate");
        }

        private Decimal convertToHrs(String jirafield)
{
            try
            {
                if (fields[jirafield] != null && fields[jirafield].ToString() != "")
                {
                    return Math.Round( Convert.ToDecimal(fields[jirafield].ToString()) / (60 * 60) , 2);
                }
                return 0;
            }
            catch (Exception ex)
            {
                return 0;
            }
}

        public decimal aggregatetimeestimate()
        {
            return convertToHrs("aggregatetimeestimate");
        }


        public decimal timeestimate()
        {
            return convertToHrs("timeestimate");
        }

        public decimal aggregatetimespent()
        {
            return convertToHrs("aggregatetimespent");
        }

        #endregion


       

        /// <summary>
        /// Start date
        /// </summary>
        /// <returns>Start date or MinValue if not defined</returns>
        public DateTime startDate()
        {
            try
            {
                if (fields["startDate"] != null && fields["startDate"].HasValues)
                    return Convert.ToDateTime(fields["startDate"]);
                else
                    return DateTime.MinValue;
            }
            catch (Exception ex)
            {
                return DateTime.MinValue;
            }
        }
        //calculate start date if not set 
        public DateTime startDateCalculated()
        {
            if (fields["duedate"] != null && fields["duedate"].HasValues)
            {
                  Decimal daysNeeded =  aggregatetimeestimate()/8;
                  return Convert.ToDateTime(fields["duedate"]).AddDays(-1 * Convert.ToInt16(daysNeeded));
             }
             return DateTime.Today;

        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns>Due date or MaxValue if not defined</returns>
        public DateTime dueDate()
        {
            try
            {
                if (fields["duedate"] != null && fields["duedate"].HasValues)
                    return Convert.ToDateTime(fields["duedate"]);
                else
                    return DateTime.MaxValue;
            }
            catch (Exception ex)
            {
                return DateTime.MaxValue;
            }
        }

        public string reporter()
        {
            if (fields["reporter"] != null)
            {
                return fields["reporter"].Value<String>("name");
            }
            return null;
        }

        public string assignee()
        {
            if (fields["assignee"] != null)
            {
                return fields["assignee"].Value<String>("name");
            }
            return null;

        }

        public string issuetype()
        {

            if (fields["issuetype"] != null)
            {
                return fields["issuetype"].Value<String>("name");
            }
            return null;
        }

        public string hyperlink()
        {
            return self;
        }

    }
}
